function header = parseInstrumentHeader(headerLines, mode)
%PARSEINSTRUMENTHEADER Parses the header lines from a SBE19/37 .cnv file.
% Returns the header information in a struct.
%
% Inputs:
%   headerLines - cell array of strings, the lines of the header section.
%   mode        - Toolbox data type mode.
%
% Outputs:
%   header      - struct containing information that was in the header
%                 section.
%
header = struct;

% there's no real structure to the header information, which
% is annoying. my approach is to use various regexes to search
% for info we want, and to ignore everything else. inefficient,
% but it's the nicest way i can think of


headerExpr1  = 'Sea-Bird (.*?) *?Data File\:';

filename = 'FileName = ';
temperatureSN = 'Temperature SN = ';
numBytesPerScan = 'Number of Bytes Per Scan = ';
numVoltageWords = 'Number of Voltage Words = ';
Latitude = 'NMEA Latitude = ';
Longitude = 'NMEA longitude = ';
SystemUTC = 'System UTC = ';
scanExpr     = 'number of scans to average = ';


exprs = {headerExpr1,filename,temperatureSN ,numBytesPerScan,...
    numVoltageWords,Latitude,Longitude,SystemUTC,scanExpr};

for k = 1:length(headerLines)
    
    % try each of the expressions
    for m = 1:length(exprs)
        tkns={};
        headerLines{k}
        exprs{m}
        if m == 1
        % until one of them matches
            tkns = regexp(headerLines{k}, exprs{m}, 'tokens')
        else
            if ~isempty(regexp(headerLines{k}, exprs{m}, 'tokens'))
                tkns = regexp(headerLines{k}, exprs{m}, 'split');
            end
        end
        if ~isempty(tkns) 
            
            % yes, ugly, but easiest way to figure out which regex we're on
            switch m
                
                % header
                case 1
                    if ~isfield(header, 'instrument_model')
                        header.instrument_model = tkns{1}{1};

                    end
                % header  inputfile
                case 2

                    header.filename = tkns{2};

                    
                % header3
                case 3
                    header.temperatureSN     =  tkns{2};
                    

                    
                % scan2
                case 4
                    header.numBytesPerScan = tkns{2};
                    %%ADDED by Loz
                    
                    
                % mem
                case 5
                    header.numVoltageWords = tkns{2};
                  
                    
                % sample
                case 6
                    header.Latitude       = tkns{2};
                    
                                
                case 7
                    header.Longitude      = tkns{2};
                    
                        
                % sample2
                case 8
                    header.SystemUTC = tkns{2};
                
                                % scan
                case 9
                    header.scanAvg = tkns{2};
            
            end
        break    
        end
        
    end
end
end
